package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.PayLinkVo;
import org.dromara.system.domain.bo.PayLinkBo;
import org.dromara.system.service.IPayLinkService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 支付链接
 * 前端访问路由地址为:/system/PayLink
 *
 * @author Gavino
 * @date 2024-11-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/PayLink")
public class PayLinkController extends BaseController {

    private final IPayLinkService payLinkService;

    /**
     * 查询支付链接列表
     */
    @SaCheckPermission("system:PayLink:list")
    @GetMapping("/list")
    public TableDataInfo<PayLinkVo> list(PayLinkBo bo, PageQuery pageQuery) {
        return payLinkService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出支付链接列表
     */
    @SaCheckPermission("system:PayLink:export")
    @Log(title = "支付链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayLinkBo bo, HttpServletResponse response) {
        List<PayLinkVo> list = payLinkService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付链接", PayLinkVo.class, response);
    }

    /**
     * 获取支付链接详细信息
     *
     * @param linkId 主键
     */
    @SaCheckPermission("system:PayLink:query")
    @GetMapping("/{linkId}")
    public R<PayLinkVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long linkId) {
        return R.ok(payLinkService.queryById(linkId));
    }

    /**
     * 新增支付链接
     */
    @SaCheckPermission("system:PayLink:add")
    @Log(title = "支付链接", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<?> add(@Validated(AddGroup.class) @RequestBody PayLinkBo bo) {
        if (bo.getExpire() == 1 && bo.getTime() < 0) {
            return R.fail("过期时间参数无效");
        }

        return payLinkService.insertByBo(bo);
    }

    /**
     * 修改支付链接
     */
    @SaCheckPermission("system:PayLink:edit")
    @Log(title = "支付链接", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayLinkBo bo) {
        return toAjax(payLinkService.updateByBo(bo));
    }

    /**
     * 删除支付链接
     *
     * @param linkIds 主键串
     */
    @SaCheckPermission("system:PayLink:remove")
    @Log(title = "支付链接", businessType = BusinessType.DELETE)
    @DeleteMapping("/{linkIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] linkIds) {
        return toAjax(payLinkService.deleteWithValidByIds(List.of(linkIds), true));
    }
}
